/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.adapter;

import java.util.List;

import com.flan.stock.R;
import com.flan.stock.bean.Stock;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 市场行情页面中，各种排行榜的Listview空间的适配器
 * @author flan
 * @date   2015年10月28日
 */
public class StockRankListViewAdapter extends BaseAdapter {
	
	private List<Stock> stockList;
	private Context context;
	
	public StockRankListViewAdapter(Context context) {
		super();
		this.context = context;
	}

	@Override
	public int getCount() {
		
		if(stockList != null){
			return stockList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(stockList != null){
			return stockList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void setDate(List<Stock> stockList){
		this.stockList = stockList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		if(convertView == null){
			
			holder = new ViewHolder();
			
			convertView = LayoutInflater.from(context).inflate(R.layout.item_lv_hs_phb, parent, false);
			holder.tv_name = (TextView) convertView.findViewById(R.id.stock_name);
			holder.tv_code = (TextView) convertView.findViewById(R.id.stock_code);
			holder.tv_price = (TextView) convertView.findViewById(R.id.stock_now_price);
			holder.tv_zdf = (TextView) convertView.findViewById(R.id.stock_zdf);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(stockList != null && stockList.size() > 0){
			Stock stock = stockList.get(position);
			
			holder.tv_name.setText(stock.getName());
			holder.tv_code.setText(stock.getCode());
			holder.tv_price.setText(stock.getNowPrice()+"");
			holder.tv_zdf.setText(stock.getClosePrice()+"%");
		}
		
		return convertView;
	}
	
	/**
	 * 用于保存ListView中重用的item视图的引用
	 * @author flan
	 * @date   2015年10月28日
	 */
	class ViewHolder {
		public TextView tv_name,tv_code,tv_price,tv_zdf;
	}

}














